<template>
  <el-dialog :model-value="visible" :append-to-body="true" :title="formModel.data.id ? '修改' : '添加'" :width="1000" center @close="cloneDialog">
    <el-form ref="ruleForm" :model="formModel.data" :rules="formModel.rules" class="demo-ruleForm" label-width="80px" @submit.prevent>
      <el-form-item label="文章类型" prop="leixing">
        <el-radio-group v-model="formModel.data.leixing">
            <el-radio v-for="item in postTypeEnums" :label="item.code"> {{ item.desc }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="标题" prop="mingcheng">
        <el-input v-model="formModel.data.mingcheng" maxlength="100" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="转载自" prop="zhuanzaiwangzhi">
        <el-input v-model="formModel.data.zhuanzaiwangzhi" maxlength="100" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <div style="width: 100%">
          <editor ref="editor" :value="formModel.data.content" @input="value => formModel.data.content = value"/>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cloneDialog()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {ElMessage} from 'element-plus';
import {defineComponent, getCurrentInstance, onMounted, reactive, ref, toRefs, watch} from 'vue';
import {useStore} from 'vuex';
import {postEdit, postSave, postView} from '@/api/post.js';
import {optionsList} from '@/api/options';
import editor from '@/components/Wangeditor/index.vue';
import UploadImage from '@/components/Upload/image.vue';
import EditTable from "@/components/EditTable.vue";
import EditTag from "@/components/EditTag.vue";
import {courseView} from "@/api/course";

export default defineComponent({
  components: {
    EditTable,EditTag, editor, UploadImage
  },
  props: {
    modelValue: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    form: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  emits: ['update:modelValue', 'updateTable'],
  setup(props, {emit}) {
    const {proxy} = getCurrentInstance();
    const visible = ref(false);
    const ruleForm = ref(null);
    const loading = ref(false);

    let lecturer = reactive({
      visible: false,
      info: {}
    })

    let formModel = reactive({
      data: {},
      rules: {
        nickname: [{required: true, mingcheng: '请输入实验名称', trigger: 'blur'}]
      }
    });


    let {modelValue, form} = toRefs(props);
    if (modelValue.value) {
      visible.value = modelValue.value;
    }
    const state = reactive({
      postTypeEnums: {}
    });
    const store = useStore();
    onMounted(() => {
      // 获取分类
      store.dispatch('GetOpts', {enumName: 'PostTypeEnum'}).then((res) => {
        state.postTypeEnums = res;
      });
    });
    // 弹窗是否要打开监控
    watch(modelValue, async(val) => {
      visible.value = val;
    });
    // form 数据监控
    watch(form, async(val) => {
      const _data = val.id? await postView(val.id) : val;
      formModel.data = {
        ..._data
      };

      if (formModel.data.zuozhes1) {
        formModel.data.zuozhes1 = JSON.parse(formModel.data.zuozhes1);
      }
      if (formModel.data.zuozhes2) {
        formModel.data.zuozhes2 = JSON.parse(formModel.data.zuozhes2);
      }

      if (formModel.data.liulanqi) {
        formModel.data.liulanqi = formModel.data.liulanqi.split(",");
      }
    });

    const resetForm = () => {
      ruleForm['value'].resetFields();
      formModel.data = {};
    };

    const cloneDialog = () => {
      visible.value = false;
      emit('update:modelValue', false);
    };

    const onSubmit = () => {
      if (loading.value === true) {
        ElMessage({type: 'warning', message: '正在保存...'});
        return;
      }
      ruleForm['value'].validate(async(valid) => {
        if (valid) {
          loading.value = true;
          let d = null;
          const data = {
            ...formModel.data
          };

          if (data.zuozhes1) {
            data.zuozhes1 = JSON.stringify(data.zuozhes1);
          }
          if (data.zuozhes2) {
            data.zuozhes2 = JSON.stringify(data.zuozhes2);
          }

          if (data.id) {
            d = await postEdit(data);
          } else {
            d = await postSave(data);
          }
          if (d) {
            ElMessage({type: 'success', message: data.id ? '修改成功' : '保存成功'});
            emit('updateTable', d);
            cloneDialog();
          }
        }
        loading.value = false;
      });
    };

    const lecturerSelect = () => {
      lecturer.visible = true;
    }

    const lecturerCallback = (info) => {
      lecturer.visible = false
      if (info != null) {
        formModel.data.zuozheName = info.lecturerName;
        formModel.data.zuozheId = info.lecturerId;
      }
    }

    // 富文本改变
    const handleChangeIntro = (value) => {
      formModel.data.introduce = value
    }

    const uploadVedio = (url) => {
      formModel.data.jianjieShipin = url
    }
    const updateZuozhes1 = (data) => {
      formModel.data.zuozhes1 = data
      console.log('updateZuozhes1', data)
    }
    const updateZuozhes2 = (data) => {
      formModel.data.zuozhes2 = data
      console.log('updateZuozhes2', data)
    }

    return {
      ...toRefs(state),
      visible,
      loading,
      lecturer,
      formModel,
      ruleForm,
      cloneDialog,
      onSubmit,
      lecturerSelect,
      lecturerCallback,
      handleChangeIntro,
      uploadVedio,
      updateZuozhes1,
      updateZuozhes2
    };
  }
});
</script>
<style>
.tabsHeight {
  height: 350px
}
.browser-item {
  display: inline-block;
  margin-right: 24px;
  height: 24px;
  width: 24px;
  vertical-align: middle;
  position: relative;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.chrome {
  background-image: url("/src/assets/brower/details-chrome.png");
}
.ie {
  background-image: url("/src/assets/brower/details-ie.png");
}
.sll {
  background-image: url("/src/assets/brower/details-360.png");
}
.firefox {
  background-image: url("/src/assets/brower/details-firefox.png");
}
.el-checkbox {
  margin-right: 0;
}
</style>

